import java.util.HashMap;

/**
 * @author LKQ
 * @date 2022/1/14 10:58
 * @description 子数组的个数，那么肯定不能排序， 前缀和 + HashMap
 */
public class FormalSolution {
    public static void main(String[] args) {

    }

    public int subarraySum(int[] nums, int k) {
        int count = 0, pre = 0;
        HashMap<Integer, Integer> mp = new HashMap<>();
        mp.put(0, 1);
        for (int i = 0; i < nums.length; i++) {
            pre += nums[i];
            if (mp.containsKey(pre - k)) {
                count += mp.get(pre - k);
            }
            mp.put(pre, mp.getOrDefault(pre, 0) + 1);
        }
        return count;
    }
}
